package com.deshan.websys.code.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.deshan.websys.code.mapper.UmsUserMapper;
import com.deshan.websys.code.model.UmsUser;
import com.deshan.websys.code.model.vo.VisitLogVO;
import com.deshan.websys.code.service.UmsUserService;
import com.deshan.websys.code.utils.BeanUtil;
import com.deshan.websys.code.utils.Constant;

@Service
public class UmsUserServiceImpl extends ServiceImpl<UmsUserMapper, UmsUser> implements UmsUserService {
	@Resource
	UmsUserMapper umsUserMapper;

	@Override
	public int saveorupdate(UmsUser umsUser) {
		QueryWrapper<UmsUser> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("username", umsUser.getUsername());
		UmsUser desumsUser = umsUserMapper.selectOne(queryWrapper);
		if (desumsUser != null) {
			return 10000;
		}
		int count = umsUserMapper.insert(umsUser);
		return count;
	}

	@Override
	public int editUser(UmsUser umsUser) {
		String id = umsUser.getId();
		int result = 0;
		if (id == null) {
			result = umsUserMapper.insert(umsUser);
		} else {
			UmsUser descUmsUser = umsUserMapper.selectById(id);
			try {
				BeanUtil.copyNotNullProperties(descUmsUser, umsUser);
				result = umsUserMapper.updateById(descUmsUser);
			} catch (Exception ex) {

			}

		}
		return result;
	}

	@Override
	public int delete(UmsUser umsUser) {
		return umsUserMapper.deleteById(umsUser.getId());
	}

	@Override
	public UmsUser findUserByName(String userName) {
		QueryWrapper<UmsUser> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("username", userName);
		UmsUser umsUser = umsUserMapper.selectOne(queryWrapper);
		return umsUser;
	}

	@Override
	public List<UmsUser> getUser() {
		QueryWrapper<UmsUser> queryWrapper = new QueryWrapper<>();
		
		List<UmsUser> umsUserlist = umsUserMapper.selectList(queryWrapper);
		return umsUserlist;
	}

	@Override
	public List<UmsUser> getServiceByLegalCode(String legalCode) {
		return list(Wrappers.lambdaQuery(UmsUser.class).eq(UmsUser::getRoleType, Constant.CUSTOMER_ROLE_TYPE)
				.eq(UmsUser::getLegalCode, legalCode));
	}


}
